package com.cocoiko.customview;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cocoiko.R;
import com.cocoiko.info.FriendInfo;

public class FriendListAdapter extends ArrayAdapter<FriendInfo> {

	Context context;
	int resource;
	ArrayList<FriendInfo> array;

	public FriendListAdapter(Context context, int textViewResourceId,
			ArrayList<FriendInfo> objects) {
		super(context, textViewResourceId, objects);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.resource = textViewResourceId;
		this.array = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub

		// create a view
		View view = convertView;
		if (view == null) {
			view = new FriendListView(context);
		}

		final FriendInfo friend = array.get(position);

		if (friend != null) {
			// add data into view in here
			final ImageView avatar = ((FriendListView) view).avatar;
			final TextView name = ((FriendListView) view).name;
			final Button follow = ((FriendListView) view).follow;

			follow.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					// TODO Auto-generated method stub
					if (friend.isKokoikoAccount()) {
						if (friend.isKokoikoFriend()) {
							// unfollow at here

							friend.setKokoikoFriend(false);
							follow
									.setBackgroundResource(R.drawable.button_blue_120x60);
							follow.setText(context.getResources().getString(
									R.string.follow_button_jp));
						} else {
							// follow at here
							friend.setKokoikoFriend(true);
							follow
									.setBackgroundResource(R.drawable.button_orange_120x60);
							follow.setText(context.getResources().getString(
									R.string.unfollow_button_jp));
						}
					} else {
						// invite

					}
				}
			});
			// view
			if (friend.getAvatar() != null) {
				avatar.setImageBitmap(friend.getAvatar());
			} else {
				Bitmap bitmap = BitmapFactory.decodeResource(context
						.getResources(), R.drawable.app_kokoiko_512px);
				avatar.setImageBitmap(bitmap);
			}

			name.setText(friend.getName());

			if (friend.isKokoikoAccount()) {
				if (friend.isKokoikoFriend()) {
					follow
							.setBackgroundResource(R.drawable.button_orange_120x60);
					follow.setText(context.getResources().getString(
							R.string.unfollow_button_jp));
				} else {
					follow.setBackgroundResource(R.drawable.button_blue_120x60);
					follow.setText(context.getResources().getString(
							R.string.follow_button_jp));
				}
			} else {
				follow.setBackgroundResource(R.drawable.button_blue_120x60);
				follow.setText(context.getResources().getString(
						R.string.invite_button_jp));
			}
		}
		// return
		return view;
	}
}
